$(function(){
    _server.UserInfo_Service().setLoginState("logout");

    //单选按钮初始化
    $(".checkbox_eula").iCheck({
        checkboxClass: "icheckbox_squaremini-blue"
    },function(){
        console.log("6");
    });

    $(document).on("blur",".input-lg",function(e){
        e.stopPropagation();
        var $this = $(this);
        if($this.val()){
            $this.siblings(".login-tips").hide();
        }else{
            $this.siblings(".login-tips").show();
        }
    });

    //初始化背景图片
    asideInit();
    function asideInit(){
        var sources = [];
        for (var i = 1; i < 4; i += 1) {
            var num = i;
            if (i < 10) {
                num = '0' + i
            }
            var url = _gl._host_res+"/res/images/aside/t_nw" + num + ".jpg";
            sources.push(url)
        }
        ;sources.sort(function() {
            return Math.random() > 0.5 ? -1 : 1
        });
        var nSlide = new NSlide("#aside-block",{
            width: window.innerWidth,
            height: window.innerHeight,
            sources: sources
        });
        $("#aside-block").on("mouseover", function() {
            nSlide.flip();
        });
        $(window).resize(_helper.throttle_duration(function(){
            $("#aside-block").find(">div").remove();
            nSlide=null;
            nSlide = new NSlide("#aside-block",{
                width: window.innerWidth,
                height: window.innerHeight,
                sources: sources
            });
        },400,800));
    }

    //忘记密码
    $(".btn-reset").on("click",function(e){
        e.stopPropagation();
        var userInfo = {"userName":"admin","password":"admin"};
        _server.UserInfo_Service().setUserInfo(userInfo);

        layer.open({
            title:"密码重置提示",
            "content":"密码已被重置为 admin"
        });
    });

    //登录
    $(".btn-login").on("click",function(e){
        e.stopPropagation();
        console.log("login");
        var $userName = $("#userName");
        if($userName.val()==""){
            $userName.siblings(".login-tips").show().find(".text").html("用户名不能为空");
            return false;
        }
        var $password = $("#password");
        if($password.val()==""){
            $password.siblings(".login-tips").show().find(".text").html("密码不能为空");
            return false;
        }

        var userInfo = _server.UserInfo_Service().getUserInfo();
        if(!userInfo) {
            userInfo = {"userName":"admin","password":"admin"};
            _server.UserInfo_Service().setUserInfo(userInfo);
        }

        if($userName.val()!=userInfo.userName){
            $userName.siblings(".login-tips").show().find(".text").html("用户名不存在");
            return false;
        }
        if($password.val()!=userInfo.password){
            $password.siblings(".login-tips").show().find(".text").html("密码不正确");
            return false;
        }

        _server.UserInfo_Service().setLoginState("login");

        $("#loginForm").submit();
    });
})